package com.example.chartview

import android.R.attr.centerX
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View


/**
 * 版权：浩博智能科技有限公司 版权所有
 * @author kevin
 * 创建日期：2021/6/30
 * 描述：
 *
 */
class TestView : View {

    private val mColorArray // 圆环渐变色
            : IntArray  = intArrayOf(Color.BLUE, Color.RED)

    private val mColorArray2 // 圆环渐变色
            : Array<Float> = arrayOf()
    constructor(context: Context, attributeSet: AttributeSet): super(context, attributeSet)
    var paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mRectF // 绘制区域
            : RectF? = null



    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        paint.strokeWidth = 20f // 线条宽度为 20 像素
        val viewWide = measuredWidth - paddingLeft - paddingRight
        val viewHigh = measuredHeight - paddingTop - paddingBottom
        val mRectLength = ((if (viewWide > viewHigh) viewHigh else viewWide) - if (paint.getStrokeWidth() > paint.getStrokeWidth())
            paint.getStrokeWidth() else paint.getStrokeWidth())
        val mRectL = paddingLeft + (viewWide - mRectLength) / 2
        val mRectT = paddingTop + (viewHigh - mRectLength) / 2

        mRectF = RectF(mRectL.toFloat(), mRectT.toFloat(), (mRectL + mRectLength).toFloat(), (mRectT + mRectLength).toFloat())

        /*
        * 进度条区域
        * */

        paint.style = Paint.Style.STROKE;
        //设置渐变色区域

        var  shader = LinearGradient(0f,0f, viewHigh.toFloat(),0f, Color.parseColor("#1126B7E3"), Color.parseColor("#ff26B7E3"), Shader.TileMode.CLAMP)
        var  shader2 = LinearGradient(0f,0f, viewHigh.toFloat(),0f, Color.parseColor("#ff92E63E"), Color.parseColor("#1192E63E"), Shader.TileMode.CLAMP)
        paint.setShader(shader)

        canvas?.drawArc(mRectF!!, 10f, 144f, false, paint)
        paint.setShader(shader2)

        canvas?.drawArc(mRectF!!, 164f, 360f-164f, false, paint)
        /*
        * 文字区域
        * */
        paint.style = Paint.Style.FILL

        paint.setShader(null)
        paint.setTextSize(25f);
//        paint.textAlign = Paint.Align.CENTER      //设置单文字居中  两行的不行
        paint.color= Color.WHITE

        var topTextRect =  Rect()
        var btmTextRect =  Rect()
        paint.getTextBounds("文案",0,"文案".length,topTextRect)

//        var height = rect.height();
        var topWidth = topTextRect.width();
        canvas?.drawText("文案",(viewWide-topWidth )/2.toFloat(),viewHigh/2.toFloat()+ 45,paint)
        Log.d("jisuan","文案"+(viewWide-topWidth )/2.toFloat())

        paint.setTextSize(55f);
        paint.getTextBounds("88",0,"88".length,btmTextRect)
        var btmWidth = btmTextRect.width();

        canvas?.drawText("88",(viewWide-btmWidth )/2.toFloat() ,viewHigh/2.toFloat() ,paint)
        Log.d("jisuan","88"+(viewWide-btmWidth )/2.toFloat())

     /*   val sweepGradient = SweepGradient(centerX.toFloat(), centerX.toFloat(), Color.GRAY,  Color.BLUE)
        paint.setShader(sweepGradient)

        canvas?.drawArc(mRectF!!, 164f, 360f - 164f, false, paint)*/
    }



}